﻿using Core.Store;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utils;
using Core.Extended;

namespace Infrastructure.ExtendedEntities
{
    public class Store_Product_WithProperties : Store_Product, IStoreProduct, IExtended<Store_Product, Store_Product_WithProperties>
    {
        public Store_Product_WithProperties()
        {
        }

        public Store_Product_WithProperties(Store_Product product, string cultureName)
        {
            ClassHelpers.CopyAllProperties<Store_Product, Store_Product_WithProperties>(product, this);
            this.CultureName = cultureName;

            

            CustomProperties = new List<Pair<string, string>>();          
            foreach (var propetry in this.Store_PropertyValue2Product)
            {
                CustomProperties.Add(new Pair<string, string>() { First = propetry.Store_PropertyValue.Store_Property.Name, Second = propetry.Store_PropertyValue.Value });
            }
        }

        public Store_Product_WithProperties MapParentClassObject(Store_Product classObject, string lang)
        {
            return new Store_Product_WithProperties(classObject, lang);
        }

        public List<Pair<string, string>> CustomProperties { get; set; }

        public bool IsFromClient { get; set; }

        private string _name;
        private string _briefDescription;
        private string _description;
        private float _cost;

        public string Name
        {
            get
            {
                if (String.IsNullOrWhiteSpace(this.CultureName) || this.Store_ProductMainProperty == null || this.Store_ProductMainProperty.Count == 0 || IsFromClient)
                {
                    return _name;
                }
                else
                {
                    return this.Store_ProductMainProperty.With(x => x.SingleOrDefault(y => y.Culture.Name == this.CultureName).Name);
                }
            }
            set
            {
                if (String.IsNullOrWhiteSpace(this.CultureName) || this.Store_ProductMainProperty == null || this.Store_ProductMainProperty.Count == 0)
                {
                    _name = value;
                }
                else
                {
                    this.Store_ProductMainProperty.SingleOrDefault(x => x.Culture.Name == CultureName).Name = value;
                }
            }
        }

        public string Description
        {
            get
            {
                if (String.IsNullOrWhiteSpace(this.CultureName) || this.Store_ProductMainProperty == null || this.Store_ProductMainProperty.Count == 0 || IsFromClient)
                {
                    return _description;
                }
                else
                {
                    return this.Store_ProductMainProperty.With(x => x.SingleOrDefault(y => y.Culture.Name == this.CultureName).Description);
                }
            }
            set
            {
                if (String.IsNullOrWhiteSpace(this.CultureName) || this.Store_ProductMainProperty == null || this.Store_ProductMainProperty.Count == 0)
                {
                    _description = value;
                }
                else
                {
                    this.Store_ProductMainProperty.SingleOrDefault(x => x.Culture.Name == CultureName).Description = value;
                }
            }
        }

        public string BriefDescription
        {
            get
            {
                if (String.IsNullOrWhiteSpace(this.CultureName) || this.Store_ProductMainProperty == null || this.Store_ProductMainProperty.Count == 0 || IsFromClient)
                {
                    return _briefDescription;
                }
                else
                {
                    return this.Store_ProductMainProperty.With(x => x.SingleOrDefault(y => y.Culture.Name == this.CultureName).BriefDescription);
                }
            }
            set
            {
                if (String.IsNullOrWhiteSpace(this.CultureName) || this.Store_ProductMainProperty == null || this.Store_ProductMainProperty.Count == 0)
                {
                    _briefDescription = value;
                }
                else
                {
                    this.Store_ProductMainProperty.SingleOrDefault(x => x.Culture.Name == CultureName).BriefDescription = value;
                }
            }
        }

        public float Cost
        {
            get
            {
                if (String.IsNullOrWhiteSpace(this.CultureName) || this.Store_ProductMainProperty == null || this.Store_ProductMainProperty.Count == 0 || this.Store_ProductMainProperty.SingleOrDefault(x => x.Culture.Name == this.CultureName) == null || IsFromClient)
                {
                    return _cost;
                }
                else
                {
                    return (float)this.Store_ProductMainProperty.SingleOrDefault(x => x.Culture.Name == this.CultureName).Cost;
                }
            }
            set
            {
                if (String.IsNullOrWhiteSpace(this.CultureName) || this.Store_ProductMainProperty == null || this.Store_ProductMainProperty.Count == 0)
                {
                    _cost = value;
                }
                else
                {
                    this.Store_ProductMainProperty.SingleOrDefault(x => x.Culture.Name == CultureName).Cost = value;
                }
            }
        }

        public string CultureName
        {
            get;
            set;
        }
    }
}
